package com.bw.zg5_demo_2.ui;

import android.view.View;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bw.Zg5_Demo_2.R;
import com.bw.cinema.ui.CinemaFragment;
import com.bw.home.HomeFragment;
import com.bw.lib_mvi.BaseActivity;
import com.bw.live.LiveFragment;
import com.bw.mine.MineFragment;
import com.bw.publish.PublishFragment;
import com.bw.zg5_demo_2.adpter.MainViewPagerAdapter;
import com.bw.zg5_demo_2.common.Const;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

@Route(path = Const.MAIN_PATH)
public class MainActivity extends BaseActivity {
    private ViewPager2 main_vp;
    private BottomNavigationView main_bottom;

    @Override
    protected void initView() {
        super.initView();
        main_vp = findViewById(R.id.main_vp);
        main_bottom = findViewById(R.id.main_bottom);

        requestPermissions(new String[]{"android.permission.READ_EXTERNAL_STORAGE",
                "android.permission.WRITE_EXTERNAL_STORAGE",
                "android.permission.RECORD_AUDIO"}, 0);
    }

    private List<Fragment> getFragment() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new CinemaFragment());
        fragmentList.add(new PublishFragment());
        fragmentList.add(new LiveFragment());
        fragmentList.add(new MineFragment());
        return fragmentList;
    }

    @Override
    protected void initData() {
        super.initData();
        List<Fragment> fragmentList = getFragment();
        MainViewPagerAdapter adapter = new MainViewPagerAdapter(this, fragmentList);
        main_vp.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        main_bottom.setOnNavigationItemSelectedListener(item -> {
            main_vp.setCurrentItem(item.getOrder());
            return true;
        });

        main_vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        main_bottom.setSelectedItemId(R.id.menu_home);
                        break;
                    case 1:
                        main_bottom.setSelectedItemId(R.id.menu_cinema);
                        break;
                    case 2:
                        main_bottom.setSelectedItemId(R.id.menu_publish);
                        break;
                    case 3:
                        main_bottom.setSelectedItemId(R.id.menu_live);
                        break;
                    case 4:
                        main_bottom.setSelectedItemId(R.id.menu_mine);
                        break;
                }
            }
        });
    }

    @NonNull
    @Override
    public View getRootLayout() {
        return null;
    }

    @Override
    protected int getLayout(int layout) {
        return R.layout.activity_main;
    }
}